<%@ page language="java" contentType="text/xml; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@page import="java.util.List"%>
<%@page import="br.com.rapidrest.demo.domain.PhoneEntry"%><jsp:useBean id="bean" scope="request" class="br.com.rapidrest.demo.web.engine.phonebook.EditPhoneBook" /> 
<engine xmlns:hl="http://www.holyavenger.org/holyavenger/1.0">
	<action id="content">
		<h1><%=bean.getMessages().get("title")%></h1>
		<h2><%=bean.getMessages().get("editPhoneBook")%></h2>
		
		<form method="post">
			<fieldset>
				<legend><%=bean.getMessages().get("editPhoneBook")%></legend>
				
				<label for="name"><%=bean.getMessages().get("name")%></label>
				<input id="name" name="\${name}" type="text" value="<%=bean.getPhoneBook().getName()%>" />
				
				<input id="save" value="<%=bean.getMessages().get("save")%>" type="button"
					hl:onclick="engine/phonebook/EditPhoneBook.save/<%=bean.getPhoneBook().getId()%>" />
			</fieldset>
		</form>
		
		<form method="post">
			<fieldset>
				<legend><%=bean.getMessages().get("newPhoneBookEntry")%></legend>
				
				<label for="entryName"><%=bean.getMessages().get("name")%></label>
				<input id="entryName" name="\${entryName}" type="text" />
				
				<label for="entryPhone"><%=bean.getMessages().get("phone")%></label>
				<input id="entryPhone" name="\${entryPhone}" type="text" />
				
				<input id="save" value="<%=bean.getMessages().get("save")%>" type="button"
					hl:onclick="engine/phonebook/EditPhoneBook.addEntry/<%=bean.getPhoneBook().getId()%>" />
			</fieldset>
		</form>
		
		<%
		List<PhoneEntry> entries = bean.getEntries();
		%>
		
		<h3><%=bean.getMessages().get("phoneBookEntries")%></h3>
		<table title="<%=bean.getMessages().get("phoneBookEntries")%>">
			<thead>
				<tr>
					<th><%=bean.getMessages().get("name")%></th>
					<th><%=bean.getMessages().get("phone")%></th>
					<th><%=bean.getMessages().get("actions")%></th>
				</tr>
			</thead>
			<tbody>
			<%
			if (entries.isEmpty()) {
			%>
				<tr>
					<td colspan="3"><%=bean.getMessages().get("noRegisteredEntries")%></td>
				</tr>
			<%
			} else {
				for (PhoneEntry entry : entries) {				
			%>
				<tr>
					<td><%=entry.getName()%></td>
					<td><%=entry.getPhone()%></td>
					<td>
						<a href="#engine/phonebook/EditPhoneEntry/<%=bean.getPhoneBook().getId()%>/<%=entry.getId()%>" title="<%=bean.getMessages().get("edit")%>">
							<%=bean.getMessages().get("edit")%>
						</a>
						<a href="#engine/phonebook/EditPhoneBook.removeEntry/<%=bean.getPhoneBook().getId()%>/<%=entry.getId()%>" title="<%=bean.getMessages().get("remove")%>"
							onclick="if (window.confirm('<%=bean.getMessages().get("confirmRemovePhoneEntry")%>')) {$HL('engine/phonebook/EditPhoneBook.removeEntry/<%=bean.getPhoneBook().getId()%>/<%=entry.getId()%>')}; return false;">
							<%=bean.getMessages().get("remove")%>
						</a>
					</td>
				</tr>
			<%
				}
			}
			%>
			</tbody>
		</table>
		
		<a href="#engine/Home" title="<%=bean.getMessages().get("back")%>">
			<%=bean.getMessages().get("back")%>
		</a>
	</action>
</engine>